﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Esb.Domain.DomainObjects;
using Esb.Domain.Factories;
using Esb.Domain.DTO;

namespace Esb.Domain.Services
{
    public class ServiceService
    {
        /// <summary>
        /// 将服务注册到Esb
        /// </summary>
        /// <param name="serviceDo"></param>
        /// <param name="userName"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool RegisterService(ServiceDo serviceDo, string userName, out string message)
        {
            try {
                ServiceFactory factory = new ServiceFactory();
                if (factory.IsRegistered(serviceDo.ApplicationId, serviceDo.ServiceRouteName))
                {
                    message = "此服务已存在，重复注册";
                    return false;
                }
                else
                {
                    //保存服务
                    factory.Add(serviceDo);

                    //记日志
                    OperationLogDo log = new OperationLogDo(userName, userName, serviceDo.Id, serviceDo.ServiceRouteName, OperationType.Register);
                    LogFactory factory2 = new LogFactory();
                    factory2.AddLog(log);
                }

                message = "";
                return true;
            }
            catch(Exception e){
                message = e.Message;
                return false;
            }
        }

        /// <summary>
        /// 修改已注册的服务信息
        /// </summary>
        /// <param name="serviceDo"></param>
        /// <param name="userName"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool UpdateService(ServiceDo serviceDo, string userName, out string message)
        {
            try {
                ServiceFactory factory = new ServiceFactory();
                //更新服务
                ServiceDo serviceDo2 = factory.GetById(serviceDo.Id);
                serviceDo2.Update(serviceDo);

                //记日志
                OperationLogDo log = new OperationLogDo(userName, userName, serviceDo.Id, serviceDo.ServiceRouteName, OperationType.Update);
                LogFactory factory2 = new LogFactory();
                factory2.AddLog(log);

                message = "";
                return true;
            }
            catch (Exception e) {
                message = e.Message;
                return false;
            }
        }

        /// <summary>
        /// 根据条件查询服务列表
        /// </summary>
        /// <param name="conditon"></param>
        /// <returns></returns>
        public List<ServiceDo> QueryServices(QueryCondition conditon)
        {
            ServiceFactory factory = new ServiceFactory();
            List<ServiceDo> result = factory.GetServicesByCondition(conditon);
            return result;
        }

        /// <summary>
        /// 根据Id查服务详情
        /// </summary>
        /// <param name="ServiceId"></param>
        /// <returns></returns>
        public ServiceDo GetServiceById(int serviceId)
        {
            ServiceFactory factory = new ServiceFactory();
            ServiceDo result = factory.GetById(serviceId);
            return result;
        }

        /// <summary>
        /// 根据Id删除服务
        /// </summary>
        /// <param name="serviceId"></param>
        /// <returns></returns>
        public bool DeleteServiceById(int serviceId, string userName)
        {
            ServiceFactory factory = new ServiceFactory();
            ServiceDo serviceDo = factory.GetById(serviceId);
            bool result = serviceDo.Delete();

            OperationLogDo log = new OperationLogDo(userName, userName, serviceDo.Id, serviceDo.ServiceRouteName, OperationType.Delete);
            LogFactory factory2 = new LogFactory();
            factory2.AddLog(log);

            return result;
        }

        /// <summary>
        /// 根据服务的id查询服务的日志
        /// </summary>
        /// <param name="serviceId"></param>
        /// <returns></returns>
        public List<OperationLogDo> QueryServiceLogs(int serviceId)
        {
            LogFactory factory = new LogFactory();
            List<OperationLogDo> result = factory.GetLogsByServiceId(serviceId);
            return result;
        }
    }
}
